﻿using System;
using DesignerHelper.Help;
using SpreadsheetGear;
using SpreadsheetGear.Commands;
using SpreadsheetGear.Windows.Forms;
using DesignerHelper.Main;
using System.Linq;

namespace DesignerHelper.Commands
{

    public static class Edit
    {
        //private IBookForm form;
        //private WorkbookView view;
        //public Edit(IBookForm form)
        //{
        //    this.form = form;
        //    view =
        //}
        public enum ClearType
        {
            All,
            Contents,
            Formats,
            Comments
        }

        public enum FillType
        {
            Right,
            Down,
            Up,
            Left
        }

        public static void Print(IBookForm form, SpreadsheetGear.Printing.PrintWhat printWhat)
        {
            WorkbookView view = form.WorkbookView;
            view.Print(printWhat, true);
        }

        public static void PrintPreview(IBookForm form, SpreadsheetGear.Printing.PrintWhat printWhat)
        {
            form.WorkbookView.PrintPreview(printWhat);
        }

        public static void ClearRangeSelection(IBookForm form, ClearType type)
        {
            IRange range = form.WorkbookView.RangeSelection;
            ClearRange(form, range, type);
        }

        public static void ClearWorksheetRange(IBookForm form, ClearType type)
        {
            IRange range = form.WorkbookView.ActiveWorksheet.Range;
            ClearRange(form, range, type);
        }

        public static ISheet CopyWorksheetBefore(IBookForm form, ISheet sheet)
        {
            WorkbookView view = form.WorkbookView;
            return view.ActiveWorksheet.CopyBefore(sheet);
        }

        public static ISheet CopyWorksheetSheet(IBookForm form, ISheet sheet)
        {
            WorkbookView view = form.WorkbookView;
            return view.ActiveWorksheet.CopyAfter(
                sheet.WorkbookSet.Workbooks.OfType<IWorkbook>().Contains(sheet.Workbook) ? sheet : view.ActiveWorksheet);
        }
        public static void DeleteActiveWorkSheet(IBookForm form)
        {
            try
            {
                form.WorkbookView.ActiveWorksheet.Delete();
            }
            catch (InvalidOperationException)
            {
                MesBox.ErrorMessage("无法删除工作簿中最后一张可见的工作表");
            }
        }

        public static void CopyRange(IBookForm form)
        {
            WorkbookView view = form.WorkbookView;
            if (view.IsEditing)
                return;
            view.ActiveCommandManager.Execute(new CommandRange.Copy(view.RangeSelection));
        }

        public static void CutRange(IBookForm form)
        {
            WorkbookView view = form.WorkbookView;

            if (view.IsEditing)
                return;
            view.ActiveCommandManager.Execute(new CommandRange.Cut(view.RangeSelection));
        }

        public static void PasteRange(IBookForm form)
        {
            WorkbookView view = form.WorkbookView;

            if (view.IsEditing)
                return;
            view.ActiveCommandManager.Execute(new CommandRange.Paste(view.RangeSelection));
        }

        public static void FillRange(IBookForm form, FillType type)
        {
            WorkbookView view = form.WorkbookView;
            view.ActiveCommandManager.Execute(new UndoableFill(view.RangeSelection, type));
        }

        public static void ReDo(IBookForm form)
        {
            WorkbookView view = form.WorkbookView;

            if (view.ActiveCommandManager.CanRedo)
                view.ActiveCommandManager.Redo();
            else
                MesBox.InfoMessage("没有可以重做的操作!");
        }

        public static void UnDo(IBookForm form)
        {
            WorkbookView view = form.WorkbookView;

            if (view.ActiveCommandManager.CanUndo)
                view.ActiveCommandManager.Undo();
            else
                MesBox.InfoMessage("没有可以撤销的操作!");
        }

        public static void InsertComment(IBookForm form)
        {
            WorkbookView view = form.WorkbookView;
            view.ActiveCommandManager.Execute(new InsertComment(view.RangeSelection));
        }

        public static void ClearComment(IBookForm form)
        {
            WorkbookView view = form.WorkbookView;
            view.DoWork(() =>
            {
                var comment = view.RangeSelection.Comment;
                if (comment != null)
                    ClearRangeSelection(form, ClearType.Comments);
            });

        }

        public static void EditComment(IBookForm form)
        {
            var view = form.WorkbookView;
            view.DoWork(() => view.RangeSelection.Comment.Visible = !view.RangeSelection.Comment.Visible);
        }


        private static void ClearRange(IBookForm form, IRange range, ClearType type)
        {
            WorkbookView view = form.WorkbookView;

            Func<Command> getCommand = () =>
            {
                switch (type)
                {
                    case ClearType.All:
                        return view.ActiveCommandManager.CreateCommandClear(range);
                    case ClearType.Contents:
                        return view.ActiveCommandManager.CreateCommandClearContents(range);
                    case ClearType.Formats:
                        return view.ActiveCommandManager.CreateCommandClearFormats(range);
                    case ClearType.Comments:
                        return new ClearComment(range);
                    default:
                        return view.ActiveCommandManager.CreateCommandClear(range);
                }
            };
            view.ActiveCommandManager.Execute(getCommand());

        }

        public static int GetBookIndex(IWorkbooks workbooks, IWorkbook workbook)
        {

            for (int i = 0; i < workbooks.Count; i++)
            {
                if (workbooks[i].Equals(workbook))
                {
                    return i;
                }
            }
            return -1;
        }

    }

}
